﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.Commerce.Contexts.Orders;
using Kooboo.Commerce.Contexts.Payments;
using Kooboo.IoC;
using Kooboo.Commerce.AddIn;
using Kooboo.Commerce.Contexts.ShopSettings;

namespace Kooboo.Commerce.Services
{
    public class OrderService : ServiceBase
    {
        public void Pay(IOrder order)
        {
            var shopSetting = this.Actor.GetShopSettings();

            var method = ObjectContainer.CreateInstances<IPaymentMethod>()
                .Where(i => i.Name == order.PaymentName)
                .FirstOrDefault();

            method.TryToPay(order, shopSetting.CurrencyISOCode);
        }

        public IOrder GetOrderForReview(Int32 orderId, Int32 customerId, Int32 productId, OrderStatus status)
        {
            if (customerId < 1 || productId < 1)
            {
                return null;
            }
            else
            {
                return this.Actor.QueryOrders().Where(o => o.Id == orderId && o.Customer != null && o.Customer.Id == customerId
                     && o.OrderStatus == status && o.OrderItems != null && o.OrderItems.Where(oi => oi.ProductId == productId).Count() > 0).FirstOrDefault();
            }
        }
    }
}
